package v3.chapter6.singleton_2_1;

public class Run {
	public static void main(String[] args) {
		MyThread t1 = new MyThread();
		MyThread t2 = new MyThread();
		MyThread t3 = new MyThread();

		t1.start();
		t2.start();
		t3.start();
	}
}

class MyThread extends Thread {

	@Override
	public void run() {
		System.out.println(MyObject.getInstance().hashCode());
	}
}

class MyObject{
	private static MyObject myObject;
	private MyObject() {}
	
	// 设置同步方法效率太低了
	// 整个方法被上锁
	synchronized public static MyObject getInstance() {
		try {
			if(myObject!=null) {
				
			}else {
				// 模拟在创建对象之前做一些准备性的工作
				Thread.sleep(3000);
				myObject=new MyObject();
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
		
		return myObject;
	}
}